//
//  NowBeShownViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/6/30.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "NowBeShownViewController.h"
#import "RGCardViewLayout.h"
#import "NowBeShowViewCell.h"
#import "RequestManager.h"
#import "NowMoviesModel.h"
#import "NowShowDetailViewController.h"



//#import "AFHTTPRequestOperation.h"
@interface NowBeShownViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,RequestDelegate>
{
    MBProgressHUD *hud;
}

@property (nonatomic ,retain)NSMutableArray *nowMoviesArr;//存放电影数据
@property (nonatomic ,retain)UICollectionView *collectionView;//创建一个collectView
@property (nonatomic ,retain)NowShowDetailViewController *nowShowVC;//详情界面的VC
@end

@implementation NowBeShownViewController

- (void)dealloc
{
    [_collectionView release];
    [_nowMoviesArr release];
    [_nowShowVC release];
    [super dealloc];
}

// 加载进度条
- (void)createdownLoadProgress{
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.color = [UIColor colorWithRed:0.616 green:0.617 blue:1.000 alpha:1.000];
    [self.view addSubview:hud];
    hud.labelText = @"(*^__^*) 嘻嘻……努力加载中..";
    //设置模式为环状进度提示框    
    [hud show:YES];
    [hud release];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // 加载进度条
    [self createdownLoadProgress];
    
//    
    [self requestData];
  
    
    // Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getCurrentTime];
}


- (void)getCurrentTime{
    
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY年MM月dd日&HH:mm:ss"];
    NSString * locationString=[dateformatter stringFromDate:senddate];
    NSArray *subStrArr = [locationString componentsSeparatedByString:@"&"];
    [dateformatter release];
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:[subStrArr firstObject] style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    [leftBarBtn release];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)createCollectionView{
    
    RGCardViewLayout *layout = [[RGCardViewLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(kScreen_width - 75, (kScreen_width - 75) * 420 / 300.0 + 5 + 100);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, kScreen_width, kScreen_height - 20) collectionViewLayout:layout];
    [layout release];
    [self.view addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
//    collectionView.backgroundColor = RandomColor;
    _collectionView.layer.contents = (id)[UIImage imageNamed:@"17478971_1358396642464.jpg"].CGImage;
    [_collectionView registerClass:[NowBeShowViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
}



#pragma mark ------dataSource的实现
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return 1;
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return _nowMoviesArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NowBeShowViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    NowMoviesModel *model = _nowMoviesArr[indexPath.section];
    cell.nowModel = model;
    //设置btn的tag值等于这个电影的id
    cell.detailBtn.tag = [model.ID integerValue];
    [cell.detailBtn addTarget:self action:@selector(detailBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
    
}

//点击《详情》按钮
- (void)detailBtnClick:(UIButton *)btn{
    
    _nowShowVC  = [[NowShowDetailViewController alloc] init];
    _nowShowVC.moiveID = btn.tag;
    [self.navigationController pushViewController:_nowShowVC animated:YES];

    
}
//点击每一个item
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NowMoviesModel *model = _nowMoviesArr[indexPath.section];
    _nowShowVC  = [[NowShowDetailViewController alloc] init];
    _nowShowVC.moiveID = [model.ID integerValue];
    [self.navigationController pushViewController:_nowShowVC animated:YES];

    
}



#pragma mark ------数据请求
- (void)requestData{
    
    RequestManager *request = [[RequestManager alloc] initWithRequestURL:@"http://api.douban.com/v2/movie/nowplaying?app_name=doubanmovie&client=e:iPhone4,1%7Cy:iPhone%20OS_6.1%7Cs:mobile%7Cf:doubanmovie_2%7Cv:3.3.1%7Cm:PP_market%7Cudid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&city=%E5%8C%97%E4%BA%AC&version=2&start=0&apikey=0df993c66c0c636e29ecbb5344252a4a" requestModel:GET parameter:nil];
    request.delegate = self;
    [request startRequest];
    
}
//请求完毕之后让代理对象执行的方法，同时把请求完整的data传给代理对象
- (void)requestDidFinishWithData:(NSData *)data{
    
    NSDictionary *mainDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
    self.nowMoviesArr = [NSMutableArray arrayWithCapacity:0];
    NSArray *NowBeShowMovieArr = mainDict[@"entries"];
    for (NSDictionary *tempDic in NowBeShowMovieArr) {
        NowMoviesModel *model = [[NowMoviesModel alloc] init];
        [model setValuesForKeysWithDictionary:tempDic];
        [_nowMoviesArr addObject:model];
        [model release];
    }
    [hud hide:YES];
    [self createCollectionView];
    [_collectionView reloadData];

}
//请求失败后把错误原因传值给代理对象
- (void)requestError:(NSError *)error{
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
